package com.czr.service.blog;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.entity.BlogCategory;
import com.czr.mapper.BlogCategoryMapper;
import com.czr.service.blog.BlogCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: czr czr
 * Version: 1.0
 * Create Date Time: 2021/11/17 16:35.
 * Update Date Time:
 *
 * @see
 */
@Service
@Slf4j
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements BlogCategoryService {


    @Override
    public List<BlogCategory> findBlogCategoryes() {
        // 查询条件
        QueryWrapper<BlogCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        //queryWrapper.orderByAsc("sorted");

        List<BlogCategory> blogCategoryList = this.list(queryWrapper);

        if (!CollectionUtils.isEmpty(blogCategoryList)) {
            // 使用stream流排序去取代SQL的排序，换取排序不走索引的问题
            blogCategoryList = blogCategoryList.stream().sorted((o1, o2) -> o1.getSorted() - o2.getSorted()).collect(Collectors.toList());
        }
        return blogCategoryList;
    }
}
